package org.bambrikii.examples.rutext.lexer.impl;

import java.util.LinkedList;
import java.util.List;

import org.bambrikii.examples.rutext.lexer.ILexer;
import org.bambrikii.examples.rutext.parser.IParser;
import org.bambrikii.examples.rutext.parser.ParserException;

public abstract class LexerBase<T> implements ILexer<T> {

	private IParser parser;
	private List<ILexer<?>> children = new LinkedList<ILexer<?>>();

	public LexerBase(IParser parser) {
		this.parser = parser;
	}

	protected boolean hasNext() throws ParserException {
		return parser.hasNext();
	}

	protected String getLast() throws ParserException {
		if (parser.hasNext()) {
			return parser.getLast();
		}
		// throw new ParserException("the end of string reached");
		return null;
	}

	protected String consume() throws ParserException {
		return parser.consume();
	}

	protected List<ILexer<?>> getChildren() {
		return children;
	}

	protected void addChild(ILexer<?> child) {
		this.children.add(child);
	}

	@Override
	public abstract T lex() throws ParserException;

	public IParser getParser() {
		return parser;
	}

}
